package com.seecen.service.impl;

import com.seecen.dao.BookCategoryDao;
import com.seecen.entity.Book;
import com.seecen.entity.BookCategory;
import com.seecen.service.BookCategoryService;
import com.seecen.service.BookService;
import com.seecen.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author bigpig
* @description 针对表【T_BOOK_CATEGORY】的数据库操作Service实现
* @createDate 2024-08-26 10:27:56
*/
@Service
public class BookCategoryServiceImpl implements BookCategoryService{

    @Autowired
    private BookCategoryDao bookCategoryDao;

    @Autowired
    private BookService bookService;

    @Override
    public void save(BookCategory bookCategory) {
        bookCategoryDao.save(bookCategory);
    }

    public void saveCateGoryAndBook(BookCategory bookCategory) {
        bookCategoryDao.saveCateGoryAndBook(bookCategory);
        bookCategory.getBooks().forEach(book -> {
            book.setBookCategory(bookCategory);
        });
        bookCategory.getBooks().forEach(book -> {
            bookService.save(book);
        });
    }

    @Override
    public Result deleteById(int id) {
        List<Book> books = bookService.findBookByBookCategoryId(id);
        if(books == null || books.isEmpty()) {
            bookCategoryDao.deleteById(id);
            return Result.success();
        }
        return Result.fail("该图书分类下还存在图书，请先删除该图书分类下的图书!");
    }

    @Override
    public void update(BookCategory bookCategory) {
        bookCategoryDao.update(bookCategory);
    }

    @Override
    public List<BookCategory> findByParam(BookCategory bookCategory) {
        return bookCategoryDao.findByParam(bookCategory);
    }
}




